#include "MainWindow.h"
#include <QMessageBox>
#include <QDebug>
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    m_myDockWidget = new MyDockWidget(this);

    removeDockWidget(ui.dockWidget);
    addDockWidget(Qt::RightDockWidgetArea, m_myDockWidget);
    splitDockWidget(m_myDockWidget, ui.dockWidget, Qt::Vertical);
    ui.dockWidget->show();

    m_pLabel = new QLabel;
    statusBar()->addWidget(m_pLabel);
    for (int i = 0; i < 255; i++) {
        m_colors << qRgb(i, i, i);
        m_colors_2 << qRgb(i, i, i);
    }
    m_colors << qRgb(255, 0, 0);        // 过爆点红色显示
    m_colors_2 << qRgb(255, 255, 255);

    m_item = nullptr;
    m_sence = new QGraphicsScene;
    ui.graphicsView->setScene(m_sence);

    m_item_2 = nullptr;
    m_sence_2 = new MyGraphicsScene;
    m_myDockWidget->setGraphicsScene(m_sence_2);

    m_queImage = new MyQueue;

    m_pThread = new CaptureThread;
    m_pThread->SetParam(this);

    QString strPicture = ":/qss/material/QinLegend.jpg";
    // QString strPicture = ":/qss/material/Winter.jpg";

    m_pThread->SetPicturePath(strPicture);
    m_pThread->start();
    ui.lineEdit->setText(strPicture);

    m_pTimer = new QTimer(this);
    connect(m_pTimer, &QTimer::timeout, this, &MainWindow::on_captured);
    m_pTimer->start(50);

    initStyleSheet();

    connect(ui.graphicsView, &MyGraphicsView::getOverBurdenEnable, this, &MainWindow::on_getOverBurdenEnable);
    connect(ui.graphicsView, &MyGraphicsView::setOverBurdenEnable, this, &MainWindow::on_setOverBurdenEnable);
    connect(ui.graphicsView, &MyGraphicsView::setPreviewRect, m_sence_2, &MyGraphicsScene::onSetPreviewRect);
    connect(m_sence_2, &MyGraphicsScene::previewRectMoved, ui.graphicsView, &MyGraphicsView::on_previewMoved);

    resizeDocks({ m_myDockWidget }, { 200 }, Qt::Horizontal);
    resizeDocks({ m_myDockWidget, ui.dockWidget }, { 300, 500 }, Qt::Vertical);
}


MainWindow::~MainWindow()
{
    while (m_pThread->isRunning()) {
        m_pThread->stop();
        QApplication::processEvents();
    }

    delete m_pThread;
    delete m_sence;
    delete m_sence_2;
    delete m_queImage;
}

// 初始化样式表
void MainWindow::initStyleSheet()
{
    //加载样式表
    QString qss;
    QFile file(":/qss/lightblue.css");
    if (file.open(QFile::ReadOnly)) {
        //用QTextStream读取样式文件不用区分文件编码 带bom也行
        QStringList list;
        QTextStream in(&file);
        //in.setCodec("utf-8");
        while (!in.atEnd()) {
            QString line;
            in >> line;
            list << line;
        }

        file.close();
        qss = list.join("\n");
        QString paletteColor = qss.mid(20, 7);
        qApp->setPalette(QPalette(paletteColor));
        qApp->setStyleSheet(qss);
    }
}

void MainWindow::on_fpsRate(int nFps)
{
    m_pLabel->setText(QString("fps: %1").arg(nFps));
}

void MainWindow::on_captured()
{
    QImage img = m_queImage->pop();
    if (img.isNull()) {
        return;
    }

    if (m_item != nullptr) {
        m_sence->removeItem(m_item);
        delete m_item;
    }
    if (m_item_2 != nullptr) {
        m_sence_2->removeItem(m_item_2);
        delete m_item_2;
    }

    QImage img_2 = img.copy();

    img.setColorTable(m_colors);
    m_item = m_sence->addPixmap(QPixmap::fromImage(img));
    ui.graphicsView->fitInView();

    img_2.setColorTable(m_colors_2);
    m_item_2 = m_sence_2->addPixmap(QPixmap::fromImage(img_2));
    m_myDockWidget->fitInView();
}

// 如果是多线程，则此槽函数必须阻塞调用
bool MainWindow::on_getOverBurdenEnable()
{
    return m_colors.last() == qRgb(255, 0, 0);
}

void MainWindow::on_setOverBurdenEnable(bool arg)
{
    m_colors.last() = arg ? qRgb(255, 0, 0) : qRgb(255, 255, 255);
}

void MainWindow::on_pushButton_clicked()
{
    QString filters("Images (*.png *.xpm *.jpg);;All files (*.*)");
    QString strPicture = QFileDialog::getOpenFileName(this, tr("打开图片文件"),
                                                    "",
                                                    filters);

    if (strPicture.isEmpty()) {
        return;
    }

    QImage img(strPicture);
    if (img.isNull()) {
        QMessageBox::warning(this, "文件类型错误", "请选择图片文件！");
        return;
    }

    ui.lineEdit->setText(strPicture);
    m_pThread->SetPicturePath(strPicture);
}


